package com.test;

import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import com.iamk.util.ImageUtil;
import com.iamk.util.ListFilesUtil;


public class CreateMask {
	public static void main(String[] args) {
		ListFilesUtil lfu = new ListFilesUtil();
		lfu.listAllFiles("D:\\University\\Ky10\\datasetImage\\MSRC_ObjCategImageDatabase_v2\\TruthTrain");
		for(int f = 0; f < lfu.arrAllFile.size();f++){
			System.out.println(lfu.arrFileName.get(f));
			BufferedImage imgGT = ImageUtil.readImage(lfu.arrAllFile.get(f));
			int[][] mask = new int[imgGT.getWidth()][imgGT.getHeight()];
			for(int i = 0; i < imgGT.getHeight(); i++){
				for(int j = 0; j < imgGT.getWidth(); j ++){
					int rgb = imgGT.getRGB(j,i);
		        	int red = rgb >>16 & 0xff;
		        	int green = rgb>>8 & 0xff;
					int blue = rgb & 0xff;
					mask[j][i] = checkLabelColor(red, green, blue);
				}
			}
			String filename = lfu.arrFileName.get(f).replace(".bmp", "").replace("_GT", "");
			saveMask("D:\\University\\Ky10\\datasetImage\\MSRC_ObjCategImageDatabase_v2\\MaskTrain7\\" + filename + ".txt", mask);
		}
	}
	
	public static int checkLabelColor(int r,int g,int  b){
		int label = -1;
//		if(r == 0 && g == 0 && b == 0) label = 0;
		if(r == 128 && g == 0 && b == 0) label = 1; 	// Building
		if(r == 0 && g == 128 && b == 0) label = 2; 	// Grass
		if(r == 128 && g == 128 && b == 0) label = 3; 	// Tree
//		if(r == 0 && g == 0 && b == 128) label = 4;  	// Cow
//		if(r == 0 && g == 128 && b == 128) label = 5; 	// Sheep
		if(r == 128 && g == 128 && b == 128) label = 4; // Sky
//		if(r == 192 && g == 0 && b == 0) label = 7;		// Aeroplane
		if(r == 64 && g == 128 && b == 0) label = 5;	// water
//		if(r == 192 && g == 128 && b == 0) label = 9;	// face
//		if(r == 64 && g == 0 && b == 128) label = 10;	// Car
//		if(r == 192 && g == 0 && b == 128) label = 11;	// Bicycle
//		if(r == 64 && g == 128 && b == 128) label = 12; // Flower
//		if(r == 192 && g == 128 && b == 128) label = 13;// sign
//		if(r == 0 && g == 64 && b == 0) label = 14;		// Bird
//		if(r == 128 && g == 64 && b == 0) label = 15;	// Book
//		if(r == 0 && g == 192 && b == 0) label = 16; 	// Chair
		if(r == 128 && g == 64 && b == 128) label = 6;	// Road
//		if(r == 0 && g == 192 && b == 128) label = 18;	// cat
//		if(r == 128 && g == 192 && b == 128) label = 19;// dog
		if(r == 64 && g == 64 && b == 0) label = 7;	// body
//		if(r == 192 && g == 64 && b == 0) label = 21;	// boat
		return label;
	}
	
	public static void saveMask(String path, int[][] mask){
		// Save mask
		int w = mask.length;
		int h = mask[0].length;
		File file = new File(path);
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(file));
			for (int i = 0; i < h; i++) {
				// for each row
				for (int j = 0; j < w; j++) // for each column
				{
					int write = (j == (w - 1)) ? 1 : 0;
					if (write == 1)
						bw.write(mask[j][i] + "");
					else
						bw.write(mask[j][i] + ",");
				}
				bw.write("\n");
			}
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
